package com.example.kaogong.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.pojo.CourseResource;
import com.example.kaogong.sys.mapper.CourseResourceMapper;
import com.example.kaogong.sys.pojo.vo.CourseResourceVo;
import com.example.kaogong.sys.service.ICourseResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程资料 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-03-26
 */
@Service
public class CourseResourceServiceImpl extends ServiceImpl<CourseResourceMapper, CourseResource> implements ICourseResourceService {

    @Autowired
    private CourseResourceMapper courseResourceMapper;

    @Value("${qiniu.url}")
    private String urlHead;

    @Override
    public List<CourseResourceVo> getCourseResourceListByCourseId(Integer courseId) {
        List<CourseResourceVo> courseResourceVoList = courseResourceMapper.getCourseResourceListByCourseId(courseId);
        courseResourceVoList.forEach(courseResourceVo -> {
            courseResourceVo.setResourcePath(urlHead + courseResourceVo.getResourcePath());
        });
        return courseResourceVoList;
    }

    /**
     * 新增课程资料
     * @param courseResource
     * @return
     */
    @Override
    public RespBean addCourseResource(CourseResource courseResource) {
        int insert = courseResourceMapper.insert(courseResource);
        if (insert < 1)
            return RespBean.error();
        return RespBean.success();
    }

    /**
     * 根据资料编号删除课程资料
     * @param resourceId
     * @return
     */
    @Override
    public RespBean deleteCourseResource(Integer resourceId) {
        int delete = courseResourceMapper.delete(new QueryWrapper<CourseResource>().eq("resource_id", resourceId));
        if (delete < 1)
            return RespBean.error();
        return RespBean.success();
    }
}
